# -*- coding: cp1251  -*-                                                                                                

from numpy import *                                                                                                     
from fractions import Fraction  

def StartTex():
    f = file(r"Out.tex", "w")
    f.write('\documentclass[12pt,a4paper]{article}\n\usepackage[english,russian,ukrainian]{babel}\n\usepackage{amsfonts,latexsym,amssymb,amsmath,amscd,amsbsy,amssymb,amsthm,enumerate,latexsym,amssymb,euscript}\n\usepackage[T2A]{fontenc}\n\usepackage[utf8]{inputenc}\n\\begin{document}\n')
    f.close()
    
def WriteNum(fraction,f):
    if fraction.denominator != 1:
    	f.write('\\frac{')
	f.write(str(fraction.numerator))
	f.write('}{')
	f.write(str(fraction.denominator))
	f.write('}')
    else:
    	f.write(str(fraction))

def WriteNumTex(fraction):
    f = file(r"Out.tex", "a") 
    WriteNum(fraction,f)
    f.close()

def WriteTex(*args):    
    f = file(r"Out.tex", "a")
    for arg in args:
    	f.write(str(arg))
    f.close()

def WriteLTex(*args):
    f = file(r"Out.tex", "a")
    for arg in args:
        f.write(str(arg))
    f.write('\n\n')
    f.close() 

def WriteArray(A):
    nraw, ncolumn = shape(A)
    f = file(r"Out.tex", "a")
    f.write('\\begin{array}{')
    for i in range(ncolumn):
    	f.write('c')
    f.write('}\n')
    for i in range(nraw):
    	WriteNum(A[i][0],f)
    	for j in range(1,ncolumn):
	    f.write(' & ')
	    WriteNum(A[i][j],f)
	f.write('\\\\'+'\n')
    f.write("\end{array}")
    f.close()

def WriteMatrix(A):
    f = file(r"Out.tex", "a")
    f.write(' $\left(')
    f.close() 
    WriteArray(A)
    f = file(r"Out.tex", "a")
    f.write('\\right) $')
    f.close() 

def WriteDetMatrix(A):
    f = file(r"Out.tex", "a")
    f.write(' $\left|')
    f.close() 
    WriteArray(A)
    f = file(r"Out.tex", "a")
    f.write('\\right| $')
    f.close() 

def CloseTex():
    f = file(r"Out.tex", "a")
    f.write('\n\end{document}')
    f.close()

